package com.LC.Controller.SystemManagement;

import com.LC.Constant.MessageConstant;
import com.LC.Entity.PageResult;
import com.LC.Entity.Permission;
import com.LC.Entity.QueryPageBean;
import com.LC.Entity.Result;
import com.LC.Service.PermissionService;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class PermissionController {
    
    @Reference
    PermissionService permissionService;
    
    @PreAuthorize("hasAuthority('PERM_ADD')")
    @PostMapping("/Permission")
    public Result addPermission(@RequestBody Permission permission){
        try {
            permissionService.addPermission(permission);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.ADD_PERMISSION_FAIL);
        }
        return new Result(true,MessageConstant.ADD_PERMISSION_SUCCESS);
    }
    
    @PreAuthorize("hasAuthority('PERM_DELETE')")
    @DeleteMapping("/Permission/{id}")
    public Result deletePermission(@PathVariable int id){
        try {
            permissionService.deletePermission(id);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.DELETE_PERMISSION_FAIL);
        }
        return new Result(true,MessageConstant.DELETE_PERMISSION_SUCCESS);
    }
    
    @PreAuthorize("hasAuthority('PERM_EDIT')")
    @PutMapping("/Permission")
    public Result updatePermission(@RequestBody Permission permission){
        try {
            permissionService.updatePermission(permission);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.EDIT_PERMISSION_FAIL);
        }
        return new Result(true,MessageConstant.EDIT_PERMISSION_SUCCESS);
    }
    
    @PreAuthorize("hasAuthority('PERM_QUERY')")
    @GetMapping("/Permission/{id}")
    public Result PermissionById(@PathVariable int id){
        Permission permission = null;
        try {
            permission = permissionService.dataById(id);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_PERMISSION_FAIL);
        }
        return new Result(true,MessageConstant.QUERY_PERMISSION_SUCCESS,permission);
    }

    @PreAuthorize("hasAuthority('PERM_QUERY')")
    @GetMapping("/Permission")
    public Result getAllPermission(){
        List<Permission> all = null;
        try {
            all = permissionService.findAll();
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_PERMISSION_FAIL);
        }
        return new Result(true,MessageConstant.QUERY_PERMISSION_SUCCESS,all);
    }
    
    @PreAuthorize("hasAuthority('PERM_QUERY')")
    @RequestMapping("/Permission/Page")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
        PageResult pageResult = permissionService.findPage(queryPageBean);
        return pageResult;
    }
}
